/*
顺序栈
*/

#ifndef ElementType
#define ElementType int
#endif

typedef struct Stack Stack;
struct Stack {
    ElementType *data;
    int top;
    int capacity;
};

// 初始化顺序栈
Stack *init(int capacity) {
    Stack *stack = new Stack;
    stack->data = new ElementType[capacity];
    stack->top = -1;
    stack->capacity = capacity;
    return stack;
}

// 判断顺序栈是否为空
bool isEmpty(Stack *stack) {
    return stack->top == -1;
}

// 判断顺序栈是否已满
bool isFull(Stack *stack) {
    return stack->top == stack->capacity - 1;
}

// 入栈
void push(Stack *stack, ElementType x) {
    if (isFull(stack)) {
        return;
    }
    stack->data[++stack->top] = x;
}

// 出栈
ElementType pop(Stack *stack) {
    if (isEmpty(stack)) {
        return -1;
    }
    return stack->data[stack->top--];
}

// 取栈顶元素
ElementType top(Stack *stack) {
    if (isEmpty(stack)) {
        return -1;
    }
    return stack->data[stack->top];
}
